<?php

class Model_transaction extends CI_Model{
	function __construct(){
        parent::__construct();
	}
	
	function getAdminTransactionFollowedList($params=array()){
		$this->db->select("t.transaction_id as transaction_id,transaction_code,p.name as name,t.price as price,t.entry_date as entry_date,f.entry_date as last_followup");
		$this->db->from("transaction_followup f");
		$this->db->join("transaction t","f.transaction_id=t.transaction_id");
		$this->db->join("product p","t.product_id=p.product_id");
		$this->db->where("f.user_id",$params['user_id']);
		$this->db->group_by("t.transaction_id");
		if(isset($params['order_by']) ) $this->db->order_by($params['order_by']);
		if(isset($params['limit_1'])  && isset($params['limit_2']) ) $this->db->limit($params['limit_1'],$params['limit_2']);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	function getTransaction($params=array()){
		$this->db->select("t.transaction_id");
		$this->db->select("t.transaction_code");
		$this->db->select("t.price");
		$this->db->select("t.entry_date");
		$this->db->select("td.jumlah_penumpang");
		$this->db->select("td.tanggal_berangkat");
		$this->db->select("td.tanggal_pulang");
		$this->db->select("p.name");
		
		$this->db->select("p.is_mice");
		$this->db->select("td.budget");
		$this->db->select("td.kota");
		$this->db->select("td.negara");
		$this->db->select("td.pesan");
		
		if(isset($params['order_detail']) ){
			$this->db->select("u.user_id");
			$this->db->select("u.username");
			$this->db->select("u.name as u_name");
			$this->db->select("u.email");
			$this->db->select("u.phone");
			$this->db->select("u.address");
			$this->db->join("user u","t.user_id=u.user_id");
		}
		
		$this->db->from("transaction t");
		$this->db->join("transaction_detail td","t.transaction_id=td.transaction_id");
		$this->db->join("product p","t.product_id=p.product_id");
		if(isset($params['transaction_id']) ) $this->db->where("t.transaction_id",$params['transaction_id']);
		//buat search nih..
		if(isset($params['keyword']) ){
			$keyword_ex = explode(" ",$params['keyword']);
			$where = "";
			foreach($keyword_ex as $keyword){
				if($where == ""){
					$where = "(";
				}else{
					$where .= " or ";
				}
				$where .= " t.transaction_code like '%".$keyword."%'";
				$where .= " or t.entry_date like '%".$keyword."%'";
				$where .= " or t.price like '%".$keyword."%'";
				$where .= " or td.jumlah_penumpang like '%".$keyword."%'";
				$where .= " or td.tanggal_berangkat like '%".$keyword."%'";
				$where .= " or td.tanggal_pulang like '%".$keyword."%'";
				$where .= " or p.name like '%".$keyword."%'";
			}
			if($where != "") $where .= ")";
			$this->db->where($where, NULL, FALSE);
		}
		
		if(isset($params['order_by']) ) $this->db->order_by($params['order_by']);
		if(isset($params['limit_1'])  && isset($params['limit_2']) ) $this->db->limit($params['limit_1'],$params['limit_2']);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	function getFollowUpList($params=array()){
		$this->db->select("tf.result");
		$this->db->select("tf.entry_date");
		$this->db->select("u.user_id");
		$this->db->select("u.username");
		$this->db->select("u.name");
		$this->db->from("transaction_followup tf");
		$this->db->join("user u","tf.user_id=u.user_id");
		if(isset($params['transaction_id']) ) $this->db->where("tf.transaction_id",$params['transaction_id']);
		if(isset($params['order_by']) ) $this->db->order_by($params['order_by']);
		if(isset($params['limit_1'])  && isset($params['limit_2']) ) $this->db->limit($params['limit_1'],$params['limit_2']);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	function addFollowup($transaction_id){
		$data = array(
			'transaction_id' => $transaction_id ,
		   	'user_id' => $this->session->userdata("user_id") ,
		   	'entry_date' => date("Y-m-d H:i:s") ,
		   	'result' => $this->input->post('result_follow',true)
		);
		
		$this->db->insert('transaction_followup', $data);

	}
}